<template>
  <div class="dashboard">
    <div class="dashboard-header">
      <h1>仪表盘</h1>
      <p class="subtitle">欢迎使用客户经理拜访系统管理控制台</p>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-grid">
      <el-card class="stat-card clickable" shadow="hover" @click="navigateToManagerDetails">
        <div class="stat-content">
          <div class="stat-icon manager">
            <el-icon><User /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-number">{{ stats.managerCount }}</div>
            <div class="stat-label">客户经理</div>
            <div class="stat-hint">点击查看详情</div>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card clickable" shadow="hover" @click="navigateToServiceDetails">
        <div class="stat-content">
          <div class="stat-icon service">
            <el-icon><Setting /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-number">{{ stats.serviceCount }}</div>
            <div class="stat-label">服务项目</div>
            <div class="stat-hint">点击查看详情</div>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-icon client">
            <el-icon><UserFilled /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-number">{{ stats.clientCount }}</div>
            <div class="stat-label">客户总数</div>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-icon worklog">
            <el-icon><Document /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-number">{{ stats.workLogCount }}</div>
            <div class="stat-label">工作日志</div>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 快速操作 -->
    <div class="quick-actions">
      <h2>快速操作</h2>
      <div class="actions-grid">
        <el-card class="action-card" shadow="hover" @click="navigateTo('/managers')">
          <div class="action-content">
            <el-icon class="action-icon"><User /></el-icon>
            <h3>管理客户经理</h3>
            <p>添加、编辑或删除客户经理账号</p>
          </div>
        </el-card>

        <el-card class="action-card" shadow="hover" @click="navigateTo('/clients')">
          <div class="action-content">
            <el-icon class="action-icon"><UserFilled /></el-icon>
            <h3>管理客户</h3>
            <p>查看、编辑客户信息，分配客户经理</p>
          </div>
        </el-card>

        <el-card class="action-card" shadow="hover" @click="navigateTo('/services')">
          <div class="action-content">
            <el-icon class="action-icon"><Setting /></el-icon>
            <h3>管理服务项目</h3>
            <p>创建和配置服务项目及其参数</p>
          </div>
        </el-card>

        <el-card class="action-card" shadow="hover" @click="navigateTo('/import')">
          <div class="action-content">
            <el-icon class="action-icon"><Upload /></el-icon>
            <h3>导入客户数据</h3>
            <p>批量导入客户名单并分配给客户经理</p>
          </div>
        </el-card>
      </div>
    </div>

    <!-- 最近活动 -->
    <div class="recent-activity">
      <h2>最近活动</h2>
      <el-card shadow="never">
        <el-timeline>
          <el-timeline-item
            v-for="activity in recentActivities"
            :key="activity.id"
            :timestamp="activity.timestamp"
            :type="activity.type"
          >
            {{ activity.description }}
          </el-timeline-item>
        </el-timeline>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import {
  User,
  Setting,
  UserFilled,
  Document,
  Upload
} from '@element-plus/icons-vue';
import { trpc } from '../../utils/trpc';

const router = useRouter();

// 统计数据
const stats = ref({
  managerCount: 0,
  serviceCount: 0,
  clientCount: 0,
  workLogCount: 0,
});

// 最近活动
const recentActivities = ref([]);

// 导航到指定页面
const navigateTo = (path: string) => {
  router.push(path);
};

// 导航到客户经理详情页面
const navigateToManagerDetails = () => {
  router.push('/manager-details');
};

// 导航到服务项目详情页面
const navigateToServiceDetails = () => {
  router.push('/service-details');
};

// 加载统计数据
const loadStats = async () => {
  try {
    console.log('开始加载统计数据...');
    const result = await trpc.stats.dashboard.query({});

    console.log('API返回结果:', result);

    if (result.success && result.data) {
      stats.value = {
        managerCount: result.data.managerCount,
        serviceCount: result.data.serviceCount,
        clientCount: result.data.clientCount,
        workLogCount: result.data.workLogCount,
      };

      // 更新最近活动
      recentActivities.value = result.data.recentActivities || [];

      console.log('统计数据加载成功:', result.data);
      ElMessage.success('统计数据加载成功');
    } else {
      console.error('获取统计数据失败:', result.message);
      ElMessage.error(result.message || '获取统计数据失败');
    }
  } catch (error) {
    console.error('加载统计数据失败:', error);
    console.error('错误详情:', error);
    ElMessage.error('加载统计数据失败，请检查网络连接');
  }
};

onMounted(() => {
  loadStats();
});
</script>

<style scoped>
.dashboard {
  max-width: 1200px;
  margin: 0 auto;
}

.dashboard-header {
  margin-bottom: 32px;
}

.dashboard-header h1 {
  margin: 0 0 8px 0;
  font-size: 28px;
  font-weight: 600;
  color: #262626;
}

.subtitle {
  margin: 0;
  color: #8c8c8c;
  font-size: 16px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 24px;
  margin-bottom: 48px;
}

.stat-card {
  border-radius: 12px;
  transition: transform 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-4px);
}

.stat-card.clickable {
  cursor: pointer;
  border: 2px solid transparent;
  transition: all 0.3s ease;
}

.stat-card.clickable:hover {
  transform: translateY(-4px);
  border-color: #1890ff;
  box-shadow: 0 8px 24px rgba(24, 144, 255, 0.12);
}

.stat-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.stat-icon {
  width: 64px;
  height: 64px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: white;
}

.stat-icon.manager {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.stat-icon.service {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stat-icon.client {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stat-icon.worklog {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-info {
  flex: 1;
}

.stat-number {
  font-size: 32px;
  font-weight: 700;
  color: #262626;
  line-height: 1;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #8c8c8c;
}

.stat-hint {
  font-size: 12px;
  color: #1890ff;
  margin-top: 2px;
}

.quick-actions {
  margin-bottom: 48px;
}

.quick-actions h2 {
  margin: 0 0 24px 0;
  font-size: 20px;
  font-weight: 600;
  color: #262626;
}

.actions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
}

.action-card {
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.action-content {
  text-align: center;
  padding: 16px;
}

.action-icon {
  font-size: 48px;
  color: #1890ff;
  margin-bottom: 16px;
}

.action-content h3 {
  margin: 0 0 8px 0;
  font-size: 18px;
  font-weight: 600;
  color: #262626;
}

.action-content p {
  margin: 0;
  color: #8c8c8c;
  font-size: 14px;
  line-height: 1.5;
}

.recent-activity h2 {
  margin: 0 0 24px 0;
  font-size: 20px;
  font-weight: 600;
  color: #262626;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .stats-grid {
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
  }
  
  .actions-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  
  .dashboard-header h1 {
    font-size: 24px;
  }
  
  .stat-number {
    font-size: 28px;
  }
}
</style>
